﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;

using WMPLib;


using MusicPlayerLib;
using MusicPlayerLib.DataModel;
using MusicPlayerLib.Utils;
using MusicPlayerLib.ViewModel;

namespace NewPandaTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //结果：当MediaPlayer指定播放的URL有错时，不播放也不会抛出异常;
            //TestWMPNull();

            //Playlist pl = PlaylistUtilsTest();
            //Console.WriteLine(pl.Tracks.Count);

            //MPlayerTest();
            // MusicUtilsTest();

            //MeidaLibraryTest();

            //GeneratePlaylists();

            GenerateMeidaLibrary();

            Console.ReadLine();
        }

        /// <summary>
        /// 测试当给定的URl有错误时WMP的处理方法;
        /// </summary>
        private static void TestWMPNull()
        {
            WindowsMediaPlayer wmPlayer = new WindowsMediaPlayer();
            try
            {
                wmPlayer.URL = @"E:\Music\May\Chris.Daughtry.-.[Daughtry].专辑.(mp3)\Chris Daughtry - Daughtry (2006)\01-daughtry-its_not_over.mp3";
                wmPlayer.controls.play();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }

            Console.ReadLine();
            wmPlayer.controls.currentPosition = 120;
            Console.ReadLine();
        }

        private static Playlist PlaylistUtilsTest()
        {
            string path = @"E:\Music\Playlist\Default.pl";

            return PlaylistUtils.LoadSingleList(path);
        }

        private static void MPlayerTest()
        {
            //创建一个播放列表模型
            PlaylistViewModel pvm = new PlaylistViewModel(PlaylistUtilsTest());

            MusicPlayer mp = new MusicPlayer(pvm);
            char ch;
            while ((ch = (char)Console.Read()) != 'q')
            {
                switch (ch)
                {
                    case 'n': mp.PlayNextTrack();
                        break;
                    case 'p': mp.PlayPreviousTrack();
                        break;
                    case 'm': mp.Mute();
                        break;
                    case 'r': mp.ReMute();
                        break;
                    case 'a': mp.PausePlayback();
                        break;
                    case 'e': mp.ResumePlayback();
                        break;
                    case 's': mp.StopPlayback();
                        break;
                    case 'd': int duration = Console.Read();
                        mp.SeekTo(duration);
                        break;
                }
            }
        }

        private static void MusicUtilsTest()
        {
            DateTime now = DateTime.Now;
            DirectoryInfo[] dirs = new DirectoryInfo(@"E:\Music\April\").GetDirectories();
            List<AudioFile> afs = new List<AudioFile>();
            for(int i=0;i<dirs.Length&&i<10;i++)
            {
                afs.AddRange(MusicPlayerUtils.GetAudioFiles(dirs[i]));
            }
          

            TimeSpan t = DateTime.Now - now;
            Console.WriteLine("Processed {0} songs, used {1} millseconds.", afs.Count, t.TotalMilliseconds);

            foreach (AudioFile af in afs)
            {
                Console.WriteLine(af.Location);
            }
        }


        private static void MeidaLibraryTest()
        {
            DirectoryInfo[] dirs = new DirectoryInfo[10];
            DirectoryInfo[] d = new DirectoryInfo(@"E:\Music\April\").GetDirectories();
            for (int i = 0; i < 10; i++)
                dirs[i] = d[i];

            MediaLibrary ml = MusicPlayerUtils.GenerateMediaLibrary(dirs);

            XmlSerializer xs = new XmlSerializer(typeof(MediaLibrary));
            FileStream fs = new FileStream(@"E:\library.xml",FileMode.OpenOrCreate);
            xs.Serialize(fs, ml);
        }


        private static void GeneratePlaylists()
        {
            DirectoryInfo[] dirs = new DirectoryInfo(@"E:\Music\April").GetDirectories();
            for (int i = 0; i < 10; i++)
            {
                Playlist pl = new Playlist();
                pl.Title = "Playlist " + i;
                pl.Tracks = MusicPlayerUtils.GetAudioFiles(dirs[i]);

                PlaylistUtils.SavePlaylist(@"E:\Music\Playlist\", pl);
            }
        }


        private static void GenerateMeidaLibrary()
        {
            string path = @"E:\Music\April";
            DirectoryInfo[] dirs = new DirectoryInfo(path).GetDirectories();

            MediaLibrary ml=MusicPlayerUtils.GenerateMediaLibrary(dirs);

            XmlSerializer xs = new XmlSerializer(typeof(MediaLibrary));
            FileStream fs=new FileStream(@"E:\Music\library.xml",FileMode.OpenOrCreate);
            xs.Serialize(fs,ml);
        }
    }
}
